project(USER_STARTER)

set(ARKTSCONFIG_PATH "${USER_STARTER_BINARY_DIR}/arktsconfig.json")
configure_file("${USER_STARTER_SOURCE_DIR}/arktsconfig.json.in" "${ARKTSCONFIG_PATH}")

compile_panda_source_list(
    TARGET_NAME user_starter_user_linker
    SOURCE_LIST
        "UserLinker.ets"
        "AppRun.ets"
        "IRun.ets"
    ARKTSCONFIG
        ${ARKTSCONFIG_PATH}
)

function(run_user_app)
    set(OPTIONS ARK_AOT)
    set(ONEVALUEARGS TARGET_NAME CLASSNAME USER_ABC_PATH)
    set(MULTIVALUEARGS EXTRA_DEPS EXTRA_BOOT_FILES EXTRA_LAUNCHER_OPTS)
    cmake_parse_arguments(PARSE_ARGV 0 ARG
        "${OPTIONS}" "${ONEVALUEARGS}" "${MULTIVALUEARGS}"
    )

    set(BOOT_PANDA_FILES
        ${USER_STARTER_BINARY_DIR}/UserLinker.abc
        ${USER_STARTER_BINARY_DIR}/AppRun.abc
        ${USER_STARTER_BINARY_DIR}/IRun.abc
        ${ARG_EXTRA_BOOT_FILES}
    )

    set(ARK_LINK_TARGET "${ARG_TARGET_NAME}_ark_link")
    set(ARK_LINK_OUTPUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/NativeLauncherPandaBoot.abc")
    ark_link(TARGET_NAME ${ARK_LINK_TARGET}
        ABC_FILE_LIST ${BOOT_PANDA_FILES}
        OUTPUT_FILE "${ARK_LINK_OUTPUT_FILE}"
        EXTRA_DEPS ${ARG_EXTRA_DEPS} user_starter_user_linker
    )

    set(TARGET_ABC_PROCCESSING "${ARG_TARGET_NAME}_abc_proccessing")
    add_custom_target(${TARGET_ABC_PROCCESSING} DEPENDS ${ARK_LINK_TARGET})

    abc2an_name("${ARK_LINK_OUTPUT_FILE}" AOT_ARK_LINK_OUTPUT_FILE)
    abc2an_name("${ARG_USER_ABC_PATH}" ARG_USER_AOT_PATH)
    if (ARG_ARK_AOT)
        set(ARK_AOT_TARGET "${ARG_TARGET_NAME}_ark_aot")
        ark_aot(TARGET_NAME "${ARK_AOT_TARGET}"
            ABC_FILE "${ARK_LINK_OUTPUT_FILE}"
            EXTRA_DEPS ${ARK_LINK_TARGET}
        )
        add_dependencies(${TARGET_ABC_PROCCESSING} ${ARK_AOT_TARGET})

        set(ARK_AOT_USER_TARGET "${ARG_TARGET_NAME}_user_ark_aot")
        ark_aot(TARGET_NAME "${ARK_AOT_USER_TARGET}"
            ABC_FILE "${ARG_USER_ABC_PATH}"
            EXTRA_DEPS ${ARK_LINK_TARGET}
            EXTRA_BOOT_PANDA_FILES
                ${ARK_LINK_OUTPUT_FILE}
        )
        add_dependencies(${TARGET_ABC_PROCCESSING} ${ARK_AOT_USER_TARGET})

        list(APPEND ARG_EXTRA_LAUNCHER_OPTS "--PandaOpt=--ext:--enable-an")
        #list(APPEND ARG_EXTRA_LAUNCHER_OPTS "--PandaOpt=--ext:--aot-files=${AOT_ARK_LINK_OUTPUT_FILE}:${ARG_USER_AOT_PATH}")
    else()
        set(ARK_REMOVE_AOT_USER_TARGET "${ARG_TARGET_NAME}_remove_aot")
        add_custom_target(${ARK_REMOVE_AOT_USER_TARGET}
            COMMAND ${CMAKE_COMMAND} -E rm -f "${AOT_ARK_LINK_OUTPUT_FILE}"
            COMMAND ${CMAKE_COMMAND} -E rm -f "${ARG_USER_AOT_PATH}"
        )

        add_dependencies(${TARGET_ABC_PROCCESSING} ${ARK_REMOVE_AOT_USER_TARGET})
    endif()


    set(NATIVE_LAUNCHER_PATH "${CMAKE_BINARY_DIR}/bin/native_launcher")

    add_custom_target(${ARG_TARGET_NAME} ALL
    COMMAND
    taskset -c 1
    ${NATIVE_LAUNCHER_PATH}
    --entry-class=AppRun.AppRun
    --entry-static-method=run
    --entry-string-arg=${ARG_CLASSNAME}:${ARG_USER_ABC_PATH}
    --PandaOpt=--ext:--boot-panda-files=${ETSSTDLIB_ABC}:${ARK_LINK_OUTPUT_FILE}
    #--PandaOpt=--ext:--log-level=debug
    #--PandaOpt=--ext:--log-components=classlinker
    ${ARG_EXTRA_LAUNCHER_OPTS}
    VERBATIM
    DEPENDS
        user_starter_user_linker
        native_launcher
        ${TARGET_ABC_PROCCESSING}
        ${ARG_EXTRA_DEPS}
    )

endfunction()

